//
//  ViewOrderViewController.m
//  Youhuiju
//
//  Created by 余尚飞 on 16/9/8.
//  Copyright © 2016年 yutenys. All rights reserved.
//

#import "ViewOrderViewController.h"
//跳转界面

//自定义视图
#import "ViewOrderTopView.h"

//自定义cell
#import "OrderStatusTableViewCell.h"//订单状态
#import "LogisticTrackingTableViewCell.h"//物流追踪
#import "ViewOrderAddressTableViewCell.h"//地址
#import "ViewOrderGoodTableViewCell.h"//商品详情
#import "ViewOrderAllCostTableViewCell.h"//确认收货，查看物流等
#import "RefundViewController.h"//申请退款



@interface ViewOrderViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)ViewOrderTopView *topView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;



@end

@implementation ViewOrderViewController

static NSString *OrderStatusTableViewCellIden = @"OrderStatusTableViewCellIden";
static NSString *LogisticTrackingTableViewCellIden = @"LogisticTrackingTableViewCellIden";
static NSString *ViewOrderAddressTableViewCellIden = @"ViewOrderAddressTableViewCellIden";
static NSString *ViewOrderGoodTableViewCellIden = @"ViewOrderGoodTableViewCellIden";
static NSString *ViewOrderAllCostTableViewCellIden = @"ViewOrderAllCostTableViewCellIden";


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self load_UI];
    
    
    // Do any additional setup after loading the view from its nib.
}


#pragma mark ---- load_UI
-(void)load_UI
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self topView];
    
    self.tableView.tableFooterView = [[UITableView alloc]init];
    [self.tableView registerNib:[UINib nibWithNibName:@"OrderStatusTableViewCell" bundle:nil] forCellReuseIdentifier:OrderStatusTableViewCellIden];
    [self.tableView registerNib:[UINib nibWithNibName:@"LogisticTrackingTableViewCell" bundle:nil] forCellReuseIdentifier:LogisticTrackingTableViewCellIden];
    [self.tableView registerNib:[UINib nibWithNibName:@"ViewOrderAddressTableViewCell" bundle:nil] forCellReuseIdentifier:ViewOrderAddressTableViewCellIden];
    [self.tableView registerNib:[UINib nibWithNibName:@"ViewOrderGoodTableViewCell" bundle:nil] forCellReuseIdentifier:ViewOrderGoodTableViewCellIden];
    [self.tableView registerNib:[UINib nibWithNibName:@"ViewOrderAllCostTableViewCell" bundle:nil] forCellReuseIdentifier:ViewOrderAllCostTableViewCellIden];
    
}

#pragma mark ---------- tableView delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;//一个为状态提示和地址，一个为商品详情
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section)
    {
        case 0:
        {
            return 3;
        }
            break;
        case 1:
        {
            return 2;//由订单内商品商量决定
        }
            break;
        default:
            return 0;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section)
    {
        case 0:
        {
            if (indexPath.row == 0)
            {
                return 100 * FitH;//状态提示图
            }else if(indexPath.row == 1){
            
                return 89 * FitH;//物流追踪提示
            }else{
            
                return 104 * FitH;//收货地址
            }
        }
            break;
        case 1:
        {
            if (indexPath.row == 1)
            {
                return 138 * FitH;//底部确认收货和订单运费等
            }
            else{
            
                return 120 * FitH;//商品详情
            }
        }
            break;
        default:
            return 0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WeakObj(self);
    switch (indexPath.section)
    {
        case 0:
        {
            if (indexPath.row == 0)//订单状态图（不可点击）
            {
                OrderStatusTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:OrderStatusTableViewCellIden forIndexPath:indexPath];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                return cell;
            }else if (indexPath.row ==1){//物流状态图（可以点击进入物流追踪详情）
            
                LogisticTrackingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:LogisticTrackingTableViewCellIden forIndexPath:indexPath];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                return cell;
            }else{//收货地址（点击后进入收货地址详情界面，可以增删改）
            
                ViewOrderAddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ViewOrderAddressTableViewCellIden forIndexPath:indexPath];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                return cell;
            }
        }
            break;
        case 1:
        {
            if (indexPath.row == 1)//设置为最后一个cell------确认收货，查看物流，申请退款
                
            {
                ViewOrderAllCostTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ViewOrderAllCostTableViewCellIden forIndexPath:indexPath];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, cell.bounds.size.width);//取消单个cell的分割线
                cell.buttonClickedHandler = ^(NSInteger buttonTag){
                    if (buttonTag == 0)
                    {
                        DLog(@"申请退款");
                      
                        RefundViewController *refund = [[RefundViewController alloc]initWithNibName:@"RefundViewController" bundle:nil];
                        [self.navigationController pushViewController:refund animated:YES];
                    }else if (buttonTag == 1){
                    
                        DLog(@"查看物流");
                    }else{
                        DLog(@"确认收货");
                    }
                    
                };
                return cell;
            }
            else{//商品参数展示（点击后进入详情页）
                ViewOrderGoodTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ViewOrderGoodTableViewCellIden forIndexPath:indexPath];
                cell.model = self.model;
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                return cell;
            }
        }
            break;
        default:
            return nil;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    switch (indexPath.section)
    {
        case 0:
        {
            if (indexPath.row == 1)//进入物流追踪详情页
            {
                DLog(@"查看物流状态");
            }
            else if (indexPath.row == 2)//进入地址详情页
            {
                DLog(@"修改地址");
            }
        }
            break;
        case 1:
        {
            if (indexPath.row != 1) {//不为最后一个cell------确认收货，查看物流，申请退款，即为商品详情页
                DLog(@"进入商品详情");
            }
        }
            break;
        default:
            break;
    }
    
//    DLog(@"别点我啦~\(≧▽≦)/~啦啦啦%zd",indexPath.row);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark ------- lazy loading
-(ViewOrderTopView *)topView
{
    WeakObj(self);
    if (!_topView)
    {
        _topView = [[ViewOrderTopView alloc]init];
        [self.view addSubview:_topView];
        [_topView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view.mas_left);
            make.right.equalTo(self.view.mas_right);
            make.top.equalTo(self.view.mas_top).offset(20);
            make.height.equalTo(@44);
        }];
        _topView.backButtonClickedHandler = ^{
            
            [weakself.navigationController popViewControllerAnimated:YES];
        };
    }
    return _topView;
}


@end
